<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--//卡片视图区域-->
    <el-card>
      <!--搜索与添加区域-->

      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.querys" clearable @clear="getUserList">
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible=true">添加用户</el-button>
        </el-col>
      </el-row>


      <!--用户列表区域-->
      <el-table :data="userList" border stripe>
        <el-table-column label="编号" type="index"></el-table-column>
        <el-table-column label="用户名" prop="username"></el-table-column>
        <el-table-column label="角色" prop="role[0][name]"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="showEditDialog(scope.row.id)"></el-button>
            <el-button type="danger" icon="el-icon-delete" @click="removeUserById(scope.row.id)"></el-button>
            <el-tooltip effect="dark" content="分配角色" placement="top">
              <el-button type="warning" icon="el-icon-setting"></el-button>
            </el-tooltip>

          </template>
        </el-table-column>
      </el-table>

     <!-- 分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

      <!--添加用户对话框-->
      <el-dialog
        title="添加用户"
        :visible.sync="addDialogVisible"
        width="50%"
        @close="addDialogClosed">
        <!--内容主体区域-->
        <el-form :model="addForm" :rules="addFromRules" ref="addFormRef" label-width="70px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="addForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="addForm.password"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="addForm.email"></el-input>
          </el-form-item>
        </el-form>
        <!--底部区域-->
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
        </span>
      </el-dialog>

      <!--修改用户对话框-->
      <el-dialog
        title="修改用户"
        :visible.sync="editDialogVisible"
        width="50%"
        @close="editDialogClosed"
        >
        <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
          <el-form-item label="用户名" >
            <el-input v-model="editForm.username" disabled></el-input>
          </el-form-item>
          <el-form-item label="邮箱"  prop="email">
            <el-input v-model="editForm.email"></el-input>
          </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editUserInfo">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
  export default {
    data(){
      return {
        queryInfo:{
          querys:'',
          pagenum:1,
          pagesize:5
        },
        userList:[],
        total:0,
        //控制对话框的显示与隐藏
        addDialogVisible:false,
        //控制修改对话框的显示与隐藏
        editDialogVisible:false,
        //查询到的用户信息
        editForm:{},
        //验证修改用户表单数据
        editFormRules:{
          email:[
            {required: true, message: '请输入邮箱', trigger: 'blur'},
            { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur'] }
          ]
        },
        //添加用户的表单数据
        addForm:{
          username:'',
          password:'',
          email:''
        },
        //添加用户数据表单验证
        addFromRules:{
          username:[
            {required: true, message: '请输入用户名称', trigger: 'blur'},
            {min:3,max:15,message: '用户名长度在3~10位之间',trigger: 'blur'}
            ],
          password:[
            {required: true, message: '请输入密码', trigger: 'blur'},
            {min:6,max:15,message: '用户名长度在6~15位之间',trigger: 'blur'}
          ],
          email:[
            {required: true, message: '请输入邮箱', trigger: 'blur'},
            { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur'] }

          ]
        }


      }
    },
    created() {
      this.getUserList()
    },
    methods:{
      getUserList(){
        this.axios.get('http://laravle-admin.com/api/admin/user',{params:this.queryInfo}).then(responce=>{
          //console.log(responce)
          if (responce.status!==200) return this.$message.error(responce.data.msg);
          this.userList=responce.data.users;
          this.total=responce.data.total;
/*          console.log(responce.data)*/
        })

      },
      handleSizeChange(newSize){
        this.queryInfo.pagesize=newSize;
        this.getUserList();
      },
      handleCurrentChange(newPage){
        this.queryInfo.pagenum=newPage;
        this.getUserList()
      },
      //监听用户对话框关闭事件
      addDialogClosed(){
        this.$refs.addFormRef.resetFields()

      },
      addUser(){
        /*预校验*/
        this.$refs.addFormRef.validate(valid=>{
          if (!valid) return
          //可以发起用户请求
          this.axios.post('http://laravle-admin.com/api/admin/users',this.addForm).then(response=>{
            /*console.log(response.status)*/
            if (response.status!==201){
              this.$message.error(response.data.msg);
            }else{
              this.$message.success(response.data.msg);
            }

            this.addDialogVisible=false;
            this.getUserList()

          })
        });
      },
      showEditDialog(id){

        this.axios.get('http://laravle-admin.com/api/admin/userd/'+id).then(responce=>{
            console.log(responce);
            if (!responce.status) return this.$message.error('查询用户失败');
            this.editForm=responce.data.data
            this.editDialogVisible=true
            console.log(this.editForm)

        })
      },
      //监听 修改表单关闭事件
      editDialogClosed(){
        this.$refs.editFormRef.resetFields()
      },
      //修改用户并提交
      editUserInfo(){
        this.$refs.editFormRef.validate(valid=>{
          if (!valid) return
          //发起修改请求
          this.axios.put('http://laravle-admin.com/api/admin/userupdate/'+this.editForm.id,{
            email:this.editForm.email
          }).then(responce=>{
            if (responce.status!==201){
              return this.$message.error('更新信息失败');
            }
            //关闭对话框
            this.editDialogVisible=false
            //刷新列表
            this.getUserList()
            //提示修改成功
            this.$message.success('更新成功');
          })
        })
      },
      //删除用户
     async removeUserById(id){
      const confirmResult=await  this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err=>err)
       if (confirmResult!=='confirm'){
         return  this.$message.info('取消了删除')

       }
       /*console.log('确认了删除')*/
       this.axios.delete('http://laravle-admin.com/api/admin/delete/'+id).then(responce=>{
         if (responce.status!==201){
           return this.$message.error('删除用户失败');
         }
         this.$message.success('删除用户成功')
         this.getUserList()
       })


      }

    }


  }
</script>
<style lang="less" scoped>

</style>
